package com.cebbank.dss.springbootredis.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author name:dss
 * @version 1.0
 * @description: TODO
 * @date 2022/9/152022/9/15
 */

@RestController
public class RedisTest {

        @Autowired
        private RedisTemplate redisTemplate;
        @Autowired
        private StringRedisTemplate stringRedisTemplate;


        @GetMapping("/hashtest")
        public void hashtest(){
            //购物车添加商品
            stringRedisTemplate.opsForHash().put("cart:1123","1001","1");
            //购物车添加商品
            stringRedisTemplate.opsForHash().put("cart:1123","1002","1");
            //购物车添加商品
            stringRedisTemplate.opsForHash().put("cart:1123","1003","1");
            //增加商品数量
            stringRedisTemplate.opsForHash().increment("cart:1123","1001",2);
            //删除商品
            stringRedisTemplate.opsForHash().delete("cart:1123","1002");
            //商品总数
            System.out.println("商品总数："+stringRedisTemplate.opsForHash().size("cart:1123"));
            //获取购物车所有商品
            System.out.println("购物车中所有商品："+stringRedisTemplate.opsForHash().entries("cart:1123"));
        }


        @GetMapping("/listtest")
        public void listtest(){
            //元素入队
            stringRedisTemplate.opsForList().rightPushAll("dss:queue","a","b","c","d","e");
            System.out.println("队列元素：");
            System.out.println(stringRedisTemplate.opsForList().range("dss:queue",0,-1));
            System.out.println("元素出队：");
            System.out.println(stringRedisTemplate.opsForList().leftPop("dss:queue",1));
            System.out.println(stringRedisTemplate.opsForList().leftPop("dss:queue",1));
            System.out.println(stringRedisTemplate.opsForList().leftPop("dss:queue",1));
            System.out.println(stringRedisTemplate.opsForList().leftPop("dss:queue",1));
            System.out.println(stringRedisTemplate.opsForList().leftPop("dss:queue",1));


            //元素入栈
            stringRedisTemplate.opsForList().rightPushAll("dss:stack","a","b","c","d","e");
            System.out.println("栈元素：");
            System.out.println(stringRedisTemplate.opsForList().range("dss:stack",0,-1));
            System.out.println("元素出栈：");
            System.out.println(stringRedisTemplate.opsForList().rightPop("dss:stack",1));
            System.out.println(stringRedisTemplate.opsForList().rightPop("dss:stack",1));
            System.out.println(stringRedisTemplate.opsForList().rightPop("dss:stack",1));
            System.out.println(stringRedisTemplate.opsForList().rightPop("dss:stack",1));
            System.out.println(stringRedisTemplate.opsForList().rightPop("dss:stack",1));
        }

    @GetMapping("/settest")
    public void settest(){
        //添加抽奖用户
        stringRedisTemplate.opsForSet().add("dss:lottery","1001","1002","1003","1004","1005","1006");
        //查看抽奖用户数量
        System.out.println("参与抽奖的用户数量为：");
        System.out.println(stringRedisTemplate.opsForSet().size("dss"));
        //查看抽奖用户
        System.out.println("参与抽奖的用户：");
        System.out.println(stringRedisTemplate.opsForSet().members("dss"));
        //抽奖
        System.out.println("中奖用户：");
        System.out.println(stringRedisTemplate.opsForSet().randomMembers("dss",2));
    }


    @GetMapping("/zsettest")
    public void zsettest(){
        //添加热搜
        stringRedisTemplate.opsForZSet().add("hotnews:20220915","台湾回归",10);
        stringRedisTemplate.opsForZSet().add("hotnews:20220915","国庆七天假",5);
        stringRedisTemplate.opsForZSet().add("hotnews:20220915","程序员的一天",3);
        stringRedisTemplate.opsForZSet().add("hotnews:20220915","山东秋季吃蟹地图",1);
        //点击热搜
        stringRedisTemplate.opsForZSet().incrementScore("hotnews:20220915","国庆七天假",2);
        stringRedisTemplate.opsForZSet().incrementScore("hotnews:20220915","台湾回归",54);
        //展示当日排行前3
        System.out.println("微博热搜排行榜：");
        System.out.println(stringRedisTemplate.opsForZSet().reverseRangeWithScores("hotnews:20220915",0,2));
    }

    @GetMapping("/stringtest")
    public void stringtest(){

        //发布文章
        stringRedisTemplate.opsForValue().set("article:001","0");
        //点击文章阅读
        stringRedisTemplate.opsForValue().increment("article:001",1);
        //点击文章阅读
        stringRedisTemplate.opsForValue().increment("article:001",3);
        //文章阅读量
        System.out.println("文章阅读量:");
        System.out.println(stringRedisTemplate.opsForValue().get("article:001"));
    }






}
